/**
 * 
 * @author Rams
 *
 */

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


class Board extends JPanel
{	
	static boolean is_your_turn = false;

	static int totalbox = 12;
	static MButton[] box = new MButton[144];

	static int sameBox = 4;
	static int satisfiedBoxs[] = new int[4];

	public Board() {
		//super(new FlowLayout(FlowLayout.CENTER,0,0));
		super();
		this.setLayout(null);
		this.setBorder(BorderFactory.createLineBorder(Color.black, 1));
		for (int i=0; i < totalbox; i++ )
		{
			//System.out.println("\n\n");
			for (int j=0; j < totalbox; j++ )
			{
				int pos = i * totalbox + j;
				int y = totalbox * MButton.width - i * MButton.width;
				int x = totalbox * MButton.height - j * MButton.height;
				//System.out.println("Pos: "+pos+" (X, Y): ("+x+","+y);
				box[pos] = new MButton(pos,i,j);
				box[pos].setPos(x-23,y-23);
				this.add(box[pos]);
				//set Position also here
			}
		}
	}

	//Create 16x16 MButton

	//Static Function to check the eligibility of a Button to set Icon.
	static boolean checkEligibility(int index) {
		if (box[index].iconset == true)
		{
			return false;
		}
		if (index < totalbox  )
		{
			return true;
		}
		else
		{
			if (box[index-totalbox].iconset == true)
			{
				return true;
			}
			return false;
		}
	}

	static void shiftArray(int arr[],int from) {
		for (int i=0; i < arr.length; i++)
		{
			if (satisfiedBoxs.length == 4)
			{
				break;
			}
			satisfiedBoxs[from++] = arr[i];
		}
	}
	
	//static function to check the same type of boxes
	static int[] getSameTypeBoxes(int index, int x, int y) {
		//get Box Position
		int m = box[index].m+x;
		int n = box[index].n+y;
		String ids = "";

		//for (int i=0; i < sameBox-1; i++)
		while(true)
		{
			if (m > -1 && n > -1 && m < totalbox && n < totalbox)
			{
				int nextBox = m * totalbox + n;;
				if (box[nextBox].belongs != 1)
				{
					break;
				}
				ids = ids+nextBox+",";
			}
			else
			{
				break;
			}
			m = m + x;
			n = n + y;
		}
		System.out.println(ids);
		if (!ids.equals(""))
		{
			String[] indeces = ids.split(",");
			int ret[] = new int[indeces.length-1];
			for (int i=0; i < indeces.length-1 ; i++)
			{
				ret[i] = Integer.parseInt(indeces[i]);
			}
			return ret;
		}
		return null;

	}
	//Static Function to check the game done
	static boolean isGameDone(int index) {
		
		int sameBoxes[];
		int sameBoxes2[];
		satisfiedBoxs[0] = index;
		//Check Left side
		sameBoxes = getSameTypeBoxes(index,1,0);
		if (sameBoxes != null && sameBoxes.length >= 3)
		{
			shiftArray(sameBoxes,1);
			return true;
		}
		//Check Right side
		sameBoxes2 = getSameTypeBoxes(index,-1,0);
		if (sameBoxes2 != null && sameBoxes2.length >= 3)
		{
			shiftArray(sameBoxes2,1);
			return true;
		}
		if (sameBoxes != null && sameBoxes2 != null && sameBoxes2.length+sameBoxes.length >= 3)
		{
			shiftArray(sameBoxes,1);
			shiftArray(sameBoxes2,sameBoxes.length+1);
			return true;
		}
		//Check Down Side
		sameBoxes = getSameTypeBoxes(index,0,-1);
		if (sameBoxes != null && sameBoxes.length >= 3)
		{
			shiftArray(sameBoxes,1);
			return true;
		}
		//Check Cross (Left-UP)
		sameBoxes = getSameTypeBoxes(index,1,1);
		if (sameBoxes != null && sameBoxes.length >= 3)
		{
			shiftArray(sameBoxes,1);
			return true;
		}
		//Check Cross (Left-Down)
		sameBoxes2 = getSameTypeBoxes(index,1,-1);
		if (sameBoxes2 != null && sameBoxes2.length >= 3)
		{
			shiftArray(sameBoxes2,1);
			return true;
		}
		if (sameBoxes != null && sameBoxes2 != null && sameBoxes2.length+sameBoxes.length >= 3)
		{
			shiftArray(sameBoxes,1);
			shiftArray(sameBoxes2,sameBoxes.length+1);
			return true;
		}
		//Check Cross (Right-UP)
		sameBoxes = getSameTypeBoxes(index,-1,1);
		if (sameBoxes != null && sameBoxes.length >= 3)
		{
			shiftArray(sameBoxes,1);
			return true;
		}
		//Check Cross (Right-Down)
		sameBoxes2 = getSameTypeBoxes(index,-1,-1);
		if (sameBoxes2 != null && sameBoxes2.length >= 3)
		{
			shiftArray(sameBoxes2,1);
			return true;
		}
		if (sameBoxes != null && sameBoxes2 != null && sameBoxes2.length+sameBoxes.length >= 3)
		{
			shiftArray(sameBoxes,1);
			shiftArray(sameBoxes2,sameBoxes.length+1);
			return true;
		}

		/*//Check Left side
		if (index % totalbox <= totalbox-sameBox)
		{
			int i;
			for (i=index; i < index+sameBox ; i++)
			{
				if (box[i].belongs != 1)
				{
					break;
				}
				satisfiedBoxs[i-index] = i; 
			}
			if (i == index+sameBox )
			{
				return true;
			}
		}
		//Check Right side
		if (index % totalbox >= sameBox-1)
		{
			int i,j = 0;
			for (i=index; i > index-sameBox ; i--)
			{
				if (box[i].belongs != 1)
				{
					break;
				}
				satisfiedBoxs[j++] = i; 
			}
			if (i == index-sameBox )
			{
				return true;
			}
		}
		//Check Down Side
		if (index >= (sameBox-1) * totalbox )
		{
			int j = index,i;
			for (i=0; i < sameBox; i++)
			{
				if (box[j].belongs != 1)
				{
					break;
				}
				satisfiedBoxs[i] = j; 
				j = j - totalbox;
			}
			if (i == sameBox )
			{
				return true;
			}
		}
		//Check Cross (Left-UP)
		if (index % totalbox <= totalbox-sameBox &&  index < (totalbox-sameBox) * totalbox )
		{
			int j = index,i;
			for (i=0; i < sameBox; i++)
			{
				if (box[j].belongs != 1)
				{
					break;
				}
				satisfiedBoxs[i] = j; 
				j = j+totalbox+1;
			}
			if (i == sameBox )
			{
				return true;
			}
		}
		//Check Cross (Left-Down)
		if (index % totalbox <= totalbox-sameBox &&  index >= (totalbox) * (sameBox-1) )
		{
			int i,j = index;
			for (i=0; i < sameBox; i++)
			{
				if (box[j].belongs != 1)
				{
					break;
				}
				satisfiedBoxs[i] = j; 
				j = j-totalbox-1;
			}
			if (i == sameBox )
			{
				return true;
			}
		}
		//Check Cross (Right-UP)
		if (index % totalbox >= sameBox-1 &&  index < (totalbox-sameBox) * totalbox )
		{
			int i,j = index;
			for (i=0; i < sameBox; i++)
			{
				if (box[j].belongs != 1)
				{
					break;
				}
				satisfiedBoxs[i] = j; 
				j = j+totalbox-1;
			}
			if (i == sameBox )
			{
				return true;
			}
		}
		//Check Cross (Right-Down)
		if (index % totalbox <= totalbox-sameBox &&  index >= (totalbox) * (sameBox-1) )
		{
			int i,j = index;
			for (i=0; i < sameBox; i++)
			{
				if (box[j].belongs != 1)
				{
					break;
				}
				satisfiedBoxs[i] = j; 
				j = j-totalbox-1;
			}
			if (i == sameBox )
			{
				return true;
			}
		}
		*/
		return false;
	}

}
